# beautifulsoup4  将复杂的html文档转为一个树形结构，每个节点都是python对象
# beautifulsoup(html)
# 获取节点：find()、find_all()/select()
# 获取属性：attrs
# 获取文本：text
from bs4 import BeautifulSoup
import requests
import re


# url = 'http://wsjkw.sc.gov.cn/scwsjkw/gzbd/fyzt.shtml'
# res = requests.get(url)
# res.encoding = 'utf-8'
# html = res.text
# soup = BeautifulSoup(html)
# print(soup.find('h2').text)
# a = soup.find('a')
# href = a.attrs['href']

# url_new = 'http://wsjkw.sc.gov.cn' + href
url_new = 'http://wsjkw.sc.gov.cn/scwsjkw/gzbd01/2020/2/16/410c4f700cf6439c9d20fa48bb3e6683.shtml'
print(url_new)
res_new = requests.get(url_new)
res_new.encoding = 'utf-8'
html_new = res_new.text
soup_new = BeautifulSoup(html_new)
p = soup_new.find(attrs={'style':'text-align: left;'})
# p = soup_new.select('body > div.wy_dis_wiap > div.wy_dis_main > div.wy_contMain.fontSt > p:nth-child(1)')
# str = p[0].get_text()
content = p.text
#使用 re 解析内容
# re 是python 自带的正则表达式模块
# re.search(regex,str)
# 1. 在 str 中查找满足条件的字符串，匹配不上返回 none
# 2. 对返回结果可以分组，可在字符串内添加小括号分离数据：
# groups()
# group(index)：返回指定分组的内容
patten = '新增确诊病例(\d+).*?出院病例(\d+).*?疑似病例(\d+).*?死亡病例(\d+)' # .*? ：加上？将匹配模式改为非贪心匹配
re_result = re.search(patten,content)
print(re_result.groups())
print(re_result.group(0))
print(re_result.group(1))
print(re_result.group(2))
print(re_result.group(3))
print(re_result.group(4))




